<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>开发部项目管理登录</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <!-- 所有页面保持相同的导航栏结构 -->

    <div class="login-container">
        <h1>开发部项目管理登录</h1>
        <form id="loginForm">
            <div class="form-group">
                <label for="username">用户名</label>
                <input type="text" id="username" name="username">
            </div>
            <div class="form-group">
                <label for="password">密码</label>
                <input type="password" id="password" name="password">
            </div>
            <div class="remember-me">
                <input type="checkbox" id="remember" name="remember">
                <label for="remember">下次登录是否记住密码？</label>
            </div>
            <button type="submit" class="login-btn">登录</button>
            <div id="errorMsg" class="error-message" style="display: none;"></div>
        </form>
    </div>
    <script src="server.js"></script>
  <script>
        document.getElementById('loginForm').addEventListener('submit', async function(e) {
            e.preventDefault();
            
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            
            try {
                const response = await fetch('/auth/login', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        username: username,
                        password: password
                    })
                });
                
                const data = await response.json();
                
                if (data.code === 200) {
                    // 保存用户信息和token到localStorage
                    localStorage.setItem('authToken', data.data.token);
                    localStorage.setItem('userInfo', JSON.stringify(data.data.user_info));
                    
                    // 跳转到项目列表页面
                    window.location.href = 'projects.html';
                } else {
                    document.getElementById('errorMsg').textContent = data.message || '登录失败';
                    document.getElementById('errorMsg').style.display = 'block';
                }
            } catch (error) {
                document.getElementById('errorMsg').textContent = '网络错误，请重试';
                document.getElementById('errorMsg').style.display = 'block';
                console.error('登录错误:', error);
            }
        });
    </script>
</body>
</html>